package org.genntii.warehousesystem.service.impl;

import com.aliyuncs.exceptions.ClientException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.genntii.warehousesystem.Exception.ParamErrorException;
import org.genntii.warehousesystem.domain.dto.GoodsDTO;
import org.genntii.warehousesystem.domain.entity.Goods;
import org.genntii.warehousesystem.domain.entity.Type;
import org.genntii.warehousesystem.domain.vo.GoodsVO;
import org.genntii.warehousesystem.mapper.BrandMapper;
import org.genntii.warehousesystem.mapper.GoodsMapper;
import org.genntii.warehousesystem.mapper.GoodsTypeMapper;
import org.genntii.warehousesystem.mapper.TypeMapper;
import org.genntii.warehousesystem.service.GoodsService;
import org.genntii.warehousesystem.utils.OssUtil;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;


@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements GoodsService {

    @Resource
    private GoodsMapper goodsMapper;

    @Resource
    private BrandMapper brandMapper;

    @Resource
    private GoodsTypeMapper goodsTypeMapper;

    @Resource
    private TypeMapper typeMapper;

    @Resource OssUtil ossUtil;

    @Override
    public void add(GoodsDTO dto){
        Goods goods = new Goods();
        goods.setName(dto.getName());
        goods.setBrand(dto.getBrandId());
//        goods.setImgSrc(OssUtil.uploadPic(file.getBytes(),dto.getName()));
        goods.setDescription(dto.getDescription());
        goods.setStatus(0);
        goods.setOnSale(0);
        goods.setOutPrice(dto.getOutPrice());
        goods.setWholesalePrice(dto.getWholesalePrice());
        goods.setWholesaleNum(dto.getWholesaleNum());
        goods.setCreateTime(LocalDateTime.now());
        goods.setLastUpdateTime(LocalDateTime.now());
        goods.setSpecification(dto.getSpecification());
        goods.setBarCode(dto.getBarCode());
        goodsMapper.insert(goods);

    }

    @Override
    public void enable(int id) {
        QueryWrapper<Goods> wrapper = new QueryWrapper<>();
        wrapper.eq("id",id);
        Goods goods = goodsMapper.selectOne(wrapper);
        if (goods == null || goods.getStatus() == 1){
            throw new ParamErrorException("参数错误");
        }
        goodsMapper.enable(id);
    }

    @Override
    public void unable(int id) {
        QueryWrapper<Goods> wrapper = new QueryWrapper<>();
        wrapper.eq("id",id);
        Goods goods = goodsMapper.selectOne(wrapper);
        if (goods == null || goods.getStatus() == 0){
            throw new ParamErrorException("参数错误");
        }
        goodsMapper.unable(id);
    }

    @Override
    public void onSale(int id) {
        QueryWrapper<Goods> wrapper = new QueryWrapper<>();
        wrapper.eq("id",id);
        Goods goods = goodsMapper.selectOne(wrapper);
        if (goods == null || goods.getOnSale() == 1){
            throw new ParamErrorException("参数错误");
        }
        goodsMapper.onSale(id);
    }

    @Override
    public void unSale(int id) {
        QueryWrapper<Goods> wrapper = new QueryWrapper<>();
        wrapper.eq("id",id);
        Goods goods = goodsMapper.selectOne(wrapper);
        if (goods == null || goods.getOnSale() == 0){
            throw new ParamErrorException("参数错误");
        }
        goodsMapper.unSale(id);
    }

    @Override
    public void update(int id, GoodsDTO dto) {
        QueryWrapper<Goods> wrapper = new QueryWrapper<>();
        wrapper.eq("id",id);
        Goods goods = goodsMapper.selectOne(wrapper);
        if (goods == null){
            throw new ParamErrorException("参数错误");
        }
        goodsMapper.update(dto.getName(),
                dto.getBrandId(),
                dto.getDescription(),
                dto.getOutPrice(),
                dto.getWholesalePrice(),
                dto.getWholesaleNum(),
                dto.getSpecification(),
                dto.getBarCode(),id);
    }

    @Override
    public List<GoodsVO> findByPage(int page) {
        List<Goods> list = goodsMapper.findByPage(10*(page-1));
        List<GoodsVO> vo = new ArrayList<>();
        for (Goods goods : list){
            GoodsVO goodsVO = new GoodsVO();
            goodsVO.setId(goods.getId());
            goodsVO.setName(goods.getName());
            goodsVO.setBrand(brandMapper.selectById(goods.getId()).getBrand());
            goodsVO.setImgSrc(goods.getImgSrc());
            goodsVO.setDescription(goods.getDescription());
            goodsVO.setOutPrice(goods.getOutPrice());
            goodsVO.setWholesalePrice(goods.getWholesalePrice());
            goodsVO.setWholesaleNum(goods.getWholesaleNum());
            goodsVO.setSpecification(goods.getSpecification());
            List<Integer> typeIds = goodsTypeMapper.selectTypeId(goods.getId());
            List<Type> types = new ArrayList<>();
            for (int i:typeIds){
                Type type = typeMapper.selectById(i);
                types.add(type);
            }
            goodsVO.setTypes(types);
            vo.add(goodsVO);
        }
        return vo;
    }

    @Override
    public List<GoodsVO> searchPage(String word, int page) {
        List<Goods> list = goodsMapper.searchPage(word,10*(page-1));
        List<GoodsVO> vo = new ArrayList<>();
        for (Goods goods : list){
            GoodsVO goodsVO = new GoodsVO();
            goodsVO.setId(goods.getId());
            goodsVO.setName(goods.getName());
            goodsVO.setBrand(brandMapper.selectById(goods.getId()).getBrand());
            goodsVO.setImgSrc(goods.getImgSrc());
            goodsVO.setDescription(goods.getDescription());
            goodsVO.setOutPrice(goods.getOutPrice());
            goodsVO.setWholesalePrice(goods.getWholesalePrice());
            goodsVO.setWholesaleNum(goods.getWholesaleNum());
            goodsVO.setSpecification(goods.getSpecification());
            List<Integer> typeIds = goodsTypeMapper.selectTypeId(goods.getId());
            List<Type> types = new ArrayList<>();
            for (int i:typeIds){
                Type type = typeMapper.selectById(i);
                types.add(type);
            }
            goodsVO.setTypes(types);
            vo.add(goodsVO);
        }
        return vo;
    }

    @Override
    public Goods GoodsDetails(int id) {
        return goodsMapper.selectById(id);
    }

    @Override
    public void setImg(int id, MultipartFile file) throws IOException, ClientException {
        String src = ossUtil.uploadPic(file,goodsMapper.selectById(id).getName());
        goodsMapper.setSrc(src,id);
    }
}
